// 使用时 import {useState} from '@/hooks/useState.js'   let stateValue=useState(["name","age","height"])  {{stateValue.name}}
//vuex 没法像选项式api那样computed里mapstate 需要借助hook
import { computed } from 'vue'
import { mapState, useStore } from 'vuex'

export function useState(mapper) {
  const store = useStore()
  //得到的是 {name:func,age:func} 这种形式
  // mapstate支持对象/数组
  const storeStateFns = mapState(mapper)
  //转变成 {name:computed ,age:computed}
  const storeState = {}
  Object.keys(storeStateFns).forEach(fnKey => {
    const fn = storeStateFns[fnKey].bind({ $store: store })
    storeState[fnKey] = computed(fn)
  })

  return storeState
}